// pages/books/detail/detail.js
var that,
  api = new (require('../../../utils/api.js')),
  headimg = api.getimgurl(),
  app = getApp(),
  util = new (require('../../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiper: [],
    already:!1,
    basicsList: [{
      icon: 'cart',
      name: '预约快递'
    }, {
      icon: 'present',
      name: '免费取件'
    }, {
      icon: 'emoji',
      name: '仓库结算'
    }, {
      icon: 'roundcheckfill',
      name: '订单完成'
    }, ],


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this;
    let id=0
    console.log(options,'optionsoptions');
    
    if(options.i){
      id=options.i
    }else if(options.id){
      id=options.id
    }else{
      wx.showModal({
        title: '提示',
        content: '无效的id',
        success:res=>{
          wx.navigateBack()
        }
      })
    }
    that.setData({ id, t: options.t||0  },()=>{
      if (app.logininfo) {
        that.getdata()
      } else {
        util.setwatcher(app, this.afterlogin)
      }
      })
    // if(options.i){
    //   that.getdata(options.i)
    // }else{
     
    // }
    // let pages = getCurrentPages(),page = pages[pages.length-2];
    // if(options.i){
    //   let book = page.data.list[options.i];
    //   this.setData({ book });
    //   this.getlist();
      
    // }
    
    // if(options.id){
    //   this.findbooksbyids(options.id);
      
    // }
   
  },
  afterlogin: {
    token: res => {
      that.getdata()
    }
  },
  getdata(){
    let appletname=app.logininfo.appletname
    wx.setNavigationBarTitle({
      title: appletname  || '小巴学堂',
    })
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
    let {id}=that.data
    api.findbookbyid(id,res=>{
      if(res.data.issuccess==1){
        that.formatdata(res.data.data);
      }
    })
  },
  toshelve(){
    wx.navigateTo({
      url: '../books',
    })
  },
  findbooksbyids(ids) {
    api.getbooks({ ids }, res => {
      if (res.data.issuccess == 1) {
        this.formatdata(res.data.data);
      }
    });
  },
  formatdata(book) {
    if (book.thumbPicPath.indexOf("http") == -1) {
      book.thumbPicPath = headimg + book.thumbPicPath;
      }
      if(book.labels!=null){
        book.labels = book.labels.split(',');
      }
      if(book.detaillist){
        book.detaillist = book.detaillist.split(',');
        if (book.detaillist.length > 0) {
          book.detaillist.forEach((y, i) => {
              if (y.indexOf("http") == -1) {
                book.detaillist[i] = headimg + book.detaillist[i];
              }
            });
          }
      }
    book.labels.unshift(`${book.agefrom}-${book.ageto}岁`)
    // if( book.labels.unshift){
    //   book.labels.unshift(`${book.agefrom}-${book.ageto}岁`)
    // }else{
    //   return
    // }

    this.setData({ book });
    wx.hideLoading();
  this.getlist();
  },
  getlist() {
    api.getcarts(res => {
      if (res.data.issuccess == 1) {
        let list = res.data.list;
        this.getgoodsid(list);
      }
    })
  },
  getgoodsid(list) {
    let goodsids = [],{book} = this.data;
    console.log(list,"list",book);
    list.forEach((x,i)=>{
      // console.log(x.id,book.id,"klsjfsdklfjs");
      if(x.goodsid == book.id){
        this.setData({already:!0});
       
      }
    });
  },

  addcart(e){
    let {goodsid} = e.currentTarget.dataset,
      { already } = this.data, cartids = wx.getStorageSync("cartids");
    already = !already;
    if(already){
      api.addcart({goodsid,type:'BOOKS'},res=>{
        if(res.data.issuccess==1){
          wx.showToast({
            title: '添加成功',
          })
          cartids.push(goodsid)
          this.setData({ already });
          wx.setStorageSync("cartids", cartids)
          app.badege(cartids);
        }
      });
    }else{
      api.delcart({ ids:goodsid }, res => {
        if (res.data.issuccess == 1) {
          wx.showToast({
            title: '取消成功',
          })
          let index = cartids.indexOf(goodsid);
          if (index > -1) {
            cartids.splice(index, 1);
          }
          this.setData({ already});
          wx.setStorageSync("cartids", cartids)
          app.badege(cartids);
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let cartids = wx.getStorageSync("cartids");
    app.badege(cartids);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return{
      title: app.logininfo.appletname,
      path: '/pages/books/detail/detail?share=1&id=' + that.data.book.id
    }
    // return {
    //   title: app.logininfo.appletname,
    //   path: `pages/listen/listen`,
    // }
  },
  onShareTimeline: () => {
    let {book} = that.data
    return {
      title: book.name,
      imageUrl: book.thumbPicPath,
      query:'id='+ that.data.book.id
    }
  },
})